﻿using System;
using System.Collections.Generic;
using TopBuilds.Restaurant.Data;

namespace TopBuilds.Restaurant.ViewModel
{
    /// <summary>
    /// 禁止对象化，直接引用其 Instance
    /// </summary>
    public sealed class FactoryCollection : System.Collections.ObjectModel.ObservableCollection<Factory>
    {
        private string _condition;

        public string Condition
        {
            get { return _condition; }

            set
            {
                if (_condition != null && _condition == value)
                {
                    return;
                }

                _condition = value;

                if (Count > 0)
                {
                    base.Clear();
                }

                Factory[] list = FactoryObject.GetFactories();
                if (list != null)
                {
                    foreach (Factory item in list)
                    {
                        Add(item);
                    }
                }
            }
        }

        /// <summary>
        /// 初始化数据, 读入全部数据记录
        /// </summary>
        public FactoryCollection()
        {
            Condition = null;
        }

        /// <summary>
        /// 由Factory Id 取得 Factory
        /// </summary>
        /// <param name="factoryId"></param>
        /// <returns></returns>
        public Factory GetFactory(int factoryId)
        {
            foreach (Factory factory in this)
            {
                if (factory.Id == factoryId)
                {
                    return factory;
                }
            }

            return null;
        }
    }
}
